$(function() {
	//Initialize favorites, etc.
	var favoriteForums = $.jStorage.get('favoriteForums', null);
	if (!favoriteForums) {
		favoriteForums = [];
		updateFavoriteForums();
	}
	
	var FORUM_LIST = $.jStorage.get('FORUM_LIST', null);
	if (!FORUM_LIST) {
		$.get('/app/mobile/forums', function(html) {
			$.jStorage.set('FORUM_LIST', html);
			$('#forumList').append(html);
			$('#forumList').listview('refresh');
		});
	}
	else {
		$('#forumList').append(FORUM_LIST);
		//$('#forumList').listview('refresh');
	}
	
	//Keep track of stuff.
	var AppState = {
		currentForum: null,
		currentForumName: null,
		currentThread: null,
		currentPage: 1
	};
	
	function updateFavoriteForums() {
		$.jStorage.set('favoriteForums', favoriteForums);
	}
	
	function addFavoriteForum() {
		favoriteForums.push({
			id: AppState.currentForum,
			name: AppState.currentForumName
		});
		
		updateFavoriteForums();
		
		alert('Forum added to favorites');
	}
	
	function deleteFavoriteForum(name) {
		var index = -1;
		var c;
		for (c = 0; c < favoriteForums.length; c++) {
			if (favoriteForums[c].name == name) {
				index = c;
				break;
			}
		}
		
		if (index != -1) {
			favoriteForums.splice(index, 1);
			updateFavoriteForums();
			if ($.mobile.activePage.is('#favorites')) {
				generateFavoritesDOM();
			}
		}
	}
	
	function generateFavoritesDOM() {
		$('#favoriteForumsList').empty();
		var linkTemplate = '<a class="forum" href="#forum" data-icon="arrow-r" data-iconpos="right"></a>';
		var list = $('#favoriteForumsList');
		
		for (c in favoriteForums) {
			var entry = $(linkTemplate);
			entry.attr('data-forum', favoriteForums[c].id);
			entry.html(favoriteForums[c].name);
			var li = $('<li></li>').append(entry);
			list.append(li);
		}
		
		list.listview('refresh');
		$('#favoritesContent').append(list);
	}
	
	$(document).ajaxError(function(event, xhr, options, thrownError) {
		alert('Error loading:\n\n' + xhr.statusText);
	});
	
	$('.forum').live('click', function() {
		$('#forumName').html('Loading forum...');
		$('#threadList').empty();
		$.mobile.pageLoading();
		
		var forumID = $(this).attr('data-forum');
		var forumName = $.trim($(this).html());
		
		$.get('/app/mobile/' + forumID, function(forumHTML) {
			AppState.currentForum = forumID;
			AppState.currentForumName = forumName;
			$('#threadList').append(forumHTML);
			$('#threadList').listview('refresh');
			$('#forumName').html(forumName);
			$.mobile.pageLoading(true);
		});
	});
	
	$('.thread').live('click', function() {
		$('#threadTitle').html('Loading thread...');
		$('#threadContent').empty();
		$.mobile.pageLoading();
		$('#loadMore').hide();
		
		var threadID = $(this).attr('data-thread');
		var threadTitle = $.trim($(this).html());
		
		$.get('/app/mobile/' + threadID, function(threadHTML) {
			AppState.currentThread = threadID;
			AppState.currentPage = 1;
			var wrapper = $('<div></div>');
			wrapper.append(threadHTML).page();
			$('#threadTitle').html(threadTitle);
			$('#threadContent').append(wrapper);
			$('#loadMore').show();
			$.mobile.pageLoading(true);
		});
	});
	
	$('#enableDeleteFavorites').click(function() {
		if (!$(this).data('deleting')) {
			$(this).data('deleting', true);
			alert('Delete mode enabled. Clicking on a favorite will delete it. Click Delete... again to disable delete mode.');
			
			$('.forum', $('#favorites')[0]).bind('click.DeleteMode', function() {
				var forumName = $.trim($(this).html());
				deleteFavoriteForum(forumName);
				return false;
			});
		}
		else {
			$(this).data('deleting', false);
			alert('No longer deleting');
			$('.forum', $('#favorites')[0]).unbind('click.DeleteMode');
		}
	});
	
	$('#loadMore').click(function() {
		AppState.currentPage++;
		var threadURL = AppState.currentThread + '-' + AppState.currentPage;
		$.get('/app/mobile/' + threadURL, function(threadHTML) {
			var wrapper = $('<div></div>');
			wrapper.append(threadHTML).page();
			$('#threadContent').append(wrapper);
			$.mobile.pageLoading(true);
		});		
	});
	
	$('#favorites').bind('pageshow', function() {
		$.mobile.pageLoading();
		generateFavoritesDOM();
		$.mobile.pageLoading(true);
	});
	
	$('#addFavoriteForum').click(addFavoriteForum);
});